from django.db import models
from apps.oaauth.models import OAUser,OADepartment

# Create your models here.
class Inform(models.Model):
    clicks = models.IntegerField(default=0)
    title = models.CharField(max_length=100)
    content = models.TextField()
    # 如果前端上传的department_ids 中包含了0  比如[0,1,2....] 那么就认为这个通知是所有部门可见
    public = models.BooleanField(default=False)
    author = models.ForeignKey(OAUser,on_delete=models.CASCADE,related_name="informs",related_query_name="informs")
    create_time = models.DateTimeField(auto_now_add=True)
    departments = models.ManyToManyField(OADepartment,related_name="informs",related_query_name='informs')

    class Meta:
        # 从大到小排序
        ordering = ('-create_time',)

class InformRead(models.Model):
    inform = models.ForeignKey(Inform,on_delete=models.CASCADE,related_name='reads',related_query_name='reads')
    user = models.ForeignKey(OAUser,on_delete=models.CASCADE,related_name='reads',related_query_name='reads')
    read_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        # 表示 inform字段和user字段合组合起来应该是唯一的
        unique_together = ('inform','user')
